package com.appbee.cm.client.nav.ui;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

import com.appbee.cm.client.nav.presenter.NavAdminPresenter;
import com.appbee.cm.client.nav.presenter.NavItemWrap;
import com.appbee.cm.client.nav.presenter.NavTreeViewModel;
import com.appbee.cm.client.nav.presenter.NavAdminPresenter.EditView;
import com.appbee.cm.client.nav.text.NavText;
import com.appbee.cm.shared.data.NavItemProxy;
import com.appbee.core.client.layout.LayoutManager;
import com.appbee.core.client.ui.IMessageView;
import com.appbee.core.client.ui.LabelIntegerbox;
import com.appbee.core.client.ui.LabelTextbox;
import com.appbee.core.client.ui.MessageView;
import com.google.gwt.core.client.GWT;
import com.google.gwt.dom.client.Element;
import com.google.gwt.dom.client.Style.Visibility;
import com.google.gwt.editor.client.Editor;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.shared.EventBus;
import com.google.gwt.text.shared.AbstractRenderer;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.cellview.client.CellTree;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.ValueListBox;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.view.client.ProvidesKey;
import com.google.gwt.view.client.SelectionChangeEvent;
import com.google.web.bindery.requestfactory.gwt.client.RequestFactoryEditorDriver;
import com.google.web.bindery.requestfactory.shared.RequestFactory;

public class NavAdminView extends Composite implements NavAdminPresenter.View,
		NavAdminPresenter.EditView, Editor<NavItemProxy>
{

	private static NavAdminViewUiBinder uiBinder = GWT
			.create(NavAdminViewUiBinder.class);

	interface NavAdminViewUiBinder extends UiBinder<Widget, NavAdminView>
	{
	}

	interface Driver extends
			RequestFactoryEditorDriver<NavItemProxy, NavAdminView>
	{
	}


	private final DialogBox popup = new DialogBox();

	@UiField
	Element editViewPanel;

	@UiField(provided = true)
	NavText navText;

	@UiField(provided = true)
	INavAdminStyle navStyle;

	@UiField(provided = true)
	CellTree navTree;

	@UiField(provided = true)
	MessageView messageView;

	@UiField
	LabelTextbox name;

	@UiField
	LabelTextbox path;

	@UiField
	LabelIntegerbox number;
	
	@UiField(provided=true)
	ValueListBox<String> displayType = new ValueListBox<String>(
			new DisplayTypeRender());

	@UiField(provided = true)
	ValueListBox<String> layoutId = new ValueListBox<String>(
			new LayoutRender(), new LayoutKeyProvider());

	@UiField
	Button saveButton;

	@UiField
	Button closeButton;

	@UiField
	Button newButton;

	private NavAdminPresenter presenter;

	private final NavCell navCell;


	@Inject
	public NavAdminView(NavText navText,
						INavAdminStyle navStyle,
						final NavTreeViewModel navTreeViewModel,
						MessageView messageView,
						NavCell navCell,
						LayoutManager layoutManager)
	{
		List<String> displayTypes = new ArrayList<String>();
		displayTypes.add("Auto");
		displayTypes.add("Hidden");
		displayType.setAcceptableValues(displayTypes);
		this.navText = navText;
		this.navStyle = navStyle;
		this.messageView = messageView;
		this.navCell = navCell;
		navTreeViewModel.setCell(navCell);
		navTree = new CellTree(navTreeViewModel, null);
		this.layoutId.setAcceptableValues(layoutManager.getLayoutIds());
		initWidget(uiBinder.createAndBindUi(this));
		popup.setWidget(this);
		popup.setText(navText.dialogTitle());
		popup.setGlassEnabled(true);
		navTreeViewModel.getSelectionModel()
				.addSelectionChangeHandler(new SelectionChangeEvent.Handler()
				{

					@Override
					public void onSelectionChange(SelectionChangeEvent event)
					{
						NavItemWrap selectedObject = navTreeViewModel
								.getSelectionModel().getSelectedObject();
						presenter.doNavItemSelected(selectedObject);
					}
				});
		saveButton.addClickHandler(new ClickHandler()
		{

			@Override
			public void onClick(ClickEvent event)
			{
				presenter.doSave();
			}
		});
		closeButton.addClickHandler(new ClickHandler()
		{

			@Override
			public void onClick(ClickEvent event)
			{
				popup.hide();
			}
		});
		newButton.addClickHandler(new ClickHandler()
		{

			@Override
			public void onClick(ClickEvent event)
			{
				presenter.doNew(null);
			}
		});
	}

	@Override
	public void show()
	{
		popup.center();
		popup.show();
	}

	@Override
	public EditView showEditView()
	{
		editViewPanel.getStyle().setVisibility(Visibility.VISIBLE);
		return this;
	}

	@Override
	public void hideEditView()
	{
		editViewPanel.getStyle().setVisibility(Visibility.HIDDEN);
	}

	@Override
	public void setPresenter(NavAdminPresenter presenter)
	{
		this.presenter = presenter;
		navCell.setPresenter(presenter);
	}

	@Override
	public IMessageView getMessageView()
	{
		return messageView;
	}


	@Override
	public RequestFactoryEditorDriver<NavItemProxy, ?> createDriver(EventBus eventBus,
																	RequestFactory rf)
	{
		Driver driver = GWT.create(Driver.class);
		driver.initialize(eventBus, rf, this);
		return driver;
	}

	@Override
	public EditView getEditView()
	{
		return this;
	}

	
	private static class LayoutRender extends AbstractRenderer<String>
	{

		@Override
		public String render(String object)
		{
			return object;
		}
		
	}
	
	private static class LayoutKeyProvider implements ProvidesKey<String>
	{

		@Override
		public Object getKey(String item)
		{
			return item;
		}
		
	}

	private class DisplayTypeRender extends AbstractRenderer<String>
	{

		@Override
		public String render(String object)
		{
			return object == null ? "" : object;
		}

	}
}
